<!--
  * 库存盘点
  *
  * @Author:    lyy
  * @Date:      2024-12-10 23:31:13
  * @Copyright  lyy
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
      <a-form-item label="耗材代码"  name="stockId">
                  <a-input style="width: 100%" v-model:value="form.materialCode" placeholder="耗材代码" />
      </a-form-item>
        <a-form-item label="盘点耗材"  name="stockId">
          <FabricMaterialsSelect style="width: 100%" v-model:value="form.stockId" placeholder="请选择" :showQuanytity="false" @getItem="getMarial"/>
<!--          <a-input style="width: 100%" v-model:value="form.stockId" placeholder="耗材id" />-->
        </a-form-item>
        <a-form-item label="盘点数量"  name="checkQuantity">
          <a-input-number style="width: 100%" v-model:value="form.checkQuantity" placeholder="清点数量" />
        </a-form-item>
        <a-form-item label="盘点人员"  name="checkedBy">
          <EmployeeSelect  width="100%" v-model:value="form.checkedBy"  placeholder="盘点人员"/>
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { inventoryCheckApi } from '/@/api/business/inventory-check/inventory-check-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import EmployeeSelect from '/@/components/system/employee-select/index.vue';
  import FabricMaterialsSelect from "/@/components/mycomponents/fabricselect/index.vue";
  import {consumablesUsageApi} from "/@/api/business/consumables-usage/consumables-usage-api.js";

  // ------------------------ 数据 ------------------------
  //耗材列表数据
  const fabricMaterial = ref();
  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    if(form.id){
      getMaterialInfo();
    }
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      materialCode: undefined, //耗材代码
      stockId: undefined, //耗材id
      checkQuantity: undefined, //清点数量
      checkedBy: undefined, //清点人
  };

  let form = reactive({ ...formDefault });

  const rules = {
      stockId: [{ required: true, message: '耗材id 必填' }],
      checkQuantity: [{ required: true, message: '清点数量 必填' }],
      checkedBy: [{ required: true, message: '清点人 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await inventoryCheckApi.update(form);
      } else {
        await inventoryCheckApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  //选择数据
  function getMarial(value){
    fabricMaterial.value = {...value}
    form.materialCode = value.fabricMaterialCode;
  }

  async function getMaterialInfo(){
    try{
      const res = await consumablesUsageApi.getMaterial(form.materialId);
      fabricMaterial.value = res.data;
    }catch (e) {
      smartSentry.captureError(e);
    }
  }

  defineExpose({
    show,
  });
</script>
